//
//  XSDrawerViewController.m
//  绿地管理系统
//
//  Created by rimi on 16/1/18.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "XSDrawerViewController.h"
enum{
    ButtonTag = 100,
    ImageTag = 200,
    Tag = 500,
};

@interface XSDrawerViewController ()

@property (nonatomic, strong) UIViewController  *mainViewController;
@property (nonatomic, strong) UIViewController  *leftViewController;
@property (nonatomic, strong) UIView            *tabBarView;

@end

@implementation XSDrawerViewController

//- (instancetype)initWithMainViewController:(UIViewController *)mainViewController leftViewController:(UIViewController *)leftViewController
//{
//    self = [super init];
//    if (self) {
//        self.mainViewController = mainViewController;
//        self.leftViewController = leftViewController;
//    }
//    return self;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addChildViewController:self.viewControllers[0]];
    [self.view addSubview:((UIViewController *)self.viewControllers[0]).view];
    [self.viewControllers[0] didMoveToParentViewController:self];
    
    [self addChildViewController:self.viewControllers[1]];
    [self.view addSubview:((UIViewController *)self.viewControllers[1]).view];
    [self.viewControllers[1] didMoveToParentViewController:self];
    

    
}

- (void)openLeftDrawer
{
    [self handleDrawerWithFlag:YES];
}

- (void)closeLeftDrawer
{
    [self handleDrawerWithFlag:NO];
}

- (void)handleDrawerWithFlag:(BOOL)isOpen
{
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    
    CGRect mainToFrame;
    //    self.mainViewController.view.frame = mainToFrame;
    CGRect leftToFrame;
    
    if (isOpen) {
        [self.view insertSubview:((UIViewController *)self.viewControllers[0]).view atIndex:0];
        leftToFrame = self.view.bounds;
        mainToFrame = CGRectMake(width - 50, 0, width, height);
    } else {
        leftToFrame = CGRectMake(-width/2, 0, width, height);
        mainToFrame = self.view.bounds;
    }
    
    [UIView animateWithDuration:0.65 animations:^{
        ((UIViewController *)self.viewControllers[0]).view.frame = leftToFrame;
        ((UIViewController *)self.viewControllers[1]).view.frame = mainToFrame;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)initializeAppearance
{
    [self.view addSubview:self.tabBarView];
    // Button
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, (375-10)/5, 50);
        button.tag = ButtonTag + i;
        [button addTarget:self action:@selector(onButton:) forControlEvents:UIControlEventTouchUpInside];
        button.center = CGPointMake((375/5/2) + i*(375/5), self.tabBarView.frame.origin.y + 25);
        [button setImage:[UIImage imageNamed:@"人脉库点击_03@2x"] forState:UIControlStateNormal];
        [self.view addSubview:button];
    }
    
}
#pragma mark -- 交互事件
- (void)onButton:(UIButton *)sender
{
    NSUInteger index = sender.tag - ButtonTag;
    self.selectedIndex = index + 1;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    UIViewController *currentViewController = self.viewControllers[_selectedIndex];
    [currentViewController.view removeFromSuperview];
    [currentViewController removeFromParentViewController];
    [currentViewController willMoveToParentViewController:nil];
    
    _selectedIndex = selectedIndex;
    
    UIViewController *selectedViewController = self.viewControllers[_selectedIndex];
    selectedViewController.view.frame = self.view.bounds;
    [self addChildViewController:selectedViewController];
    [self.view insertSubview:selectedViewController.view atIndex:1];
    [selectedViewController didMoveToParentViewController:self];
    
}

- (void)setViewControllers:(NSArray *)viewControllers
{
    _viewControllers = viewControllers;
    [self initializeAppearance];
}
#pragma mark -- getter
- (UIView *)tabBarView
{
    if (_tabBarView == nil) {
        _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 667 - 50, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        _tabBarView.backgroundColor = [UIColor colorWithRed:25/255.0 green:25/255.0 blue:25/255.0 alpha:1.0];
    }
    return _tabBarView;
}

@end













